package strategies;


import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;

import strategies.Strategy;

import Data.HibernateUtil;

/**
 * responsible  for the plugins
 * 
 * @author Vladimir Sarkisyan & Erez Levy
 *
 */
public class PluginsAPI {

	//the strategies that have in the program
	ArrayList<Strategy> strategiesList;
	
	public static PluginsAPI instance = new PluginsAPI();
	
	private PluginsAPI(){
		strategiesList = new ArrayList<Strategy>();
		
		/*
		TagBySize tagBySize = new TagBySize();
		tagBySize.setStrategyName("TagBySize");
		strategiesList.add(tagBySize);
		
		TagByType tagByType = new TagByType();
		tagByType.setStrategyName("TagByType");		
		strategiesList.add(tagByType);
		
		TagByLastModified tagByLastModified =new TagByLastModified();
		tagByLastModified.setStrategyName("TagByLastModified");
		strategiesList.add(tagByLastModified);
		*/
		// create a watcher for the plugins in new thread
		
		//the path of the plugins 
        File f = new File("");
        String path = new String(f.getAbsolutePath());
        //now path direct on directory plugins
        path = path + "\\plugins";
        Path dir = Paths.get(path);
         /*
        try {
			WatchPlugin watchPlugin = new WatchPlugin(dir, false);
			//run it in a new thread
			new Thread(watchPlugin).start();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   */  
	}
	//end of the constructor
	
	
	/*
	//add a strategy to the list
	public void addStrategyToList(Strategy strategy){
		strategiesList.add(strategy);
	}
	*/
	
	//add a strategy to the list
	public void addStrategyToList(String strategy){
		//get the strategy Instance
		Strategy strategyInstance;
		try {		
			strategyInstance = new PluginChooser(strategy).getStrategyInstanse();			
			strategiesList.add(strategyInstance);		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	/**
	 * the function get a file name and tag him by all the Strategies
	 * that have in the system - that user choose 
	 * 
	 * @param fileName - the name of file
	 */
	public void tagFileByAllTheStrategies(String fileName){
		int len = strategiesList.size();
		for(int i=0; i<len; i++){
			HibernateUtil.instance.tagFileByStrategy(fileName, strategiesList.get(i));
		}
	}
	
	/**
	 * the function return the tags of the Strategy
	 * 
	 *  the function get the name of the strategy class, after create
	 *  a PluginChooser and call to getStrategyInstanse for get the
	 *  instance of the Strategy, in this instance of Strategy call to function 
	 *  dataForGUI that return the list of tags that was in the StrategyInstance
	 *  
	 * @param strategyStr - the name of the strategy class
	 */
	//data for the GUI
	public ArrayList<String> getStrategyTags(String strategyStr) {
		//get the strategy Instance
		Strategy StrategyInstance;
		try {
			//for debug 
			System.out.println("in PluginsAPI - getStrategyTags");
			System.out.println("strategyName: " + strategyStr);
			//
			//***********************************************************
			StrategyInstance = new PluginChooser(strategyStr).getStrategyInstanse();
			
			//StrategyInstance = getStrategy(strategyStr);
			//get the tags and return them
			ArrayList<String> list = StrategyInstance.dataForGUI();
			return list;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	// end of getStrategyTags
	
	
	
	/**
	 * tag all the files by some strategy
	 * 
	 *  the function get the name of the strategy class, after create
	 *  a PluginChooser and call to getStrategyInstanse for get the
	 *  instance of the Strategy, and call to function in HibernateUtil
	 *  that tag all the files
	 *  
	 * @param strategyStr 
	 */
	public void tagAllTheFiles(String strategyStr){
		//get the strategy Instance
		Strategy strategyInstance;
		try {	
			//***********************************************************
			strategyInstance = new PluginChooser(strategyStr).getStrategyInstanse();
			
			//for debuging 
			System.out.println("in PluginsAPI - tagAllTheFiles");
			//
			
			//strategyInstance = getStrategy(strategyStr);
			//for debuging 
			System.out.println("strategyInstance - " + strategyInstance.getStrategyName());
			//
			
			//call to Hibernate and tag all the files there
			HibernateUtil.instance.tagByStrategyAllTheFiles(strategyInstance);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//end of tagAllTheFiles
	
	
	
	/**
	 * the function search files by strategy and the wanted tag from this strategy
	 * 
	 *  the function get the name of the strategy class, after create
	 *  a PluginChooser and call to getStrategyInstanse for get the
	 *  instance of the Strategy, in this instance of Strategy call to function 
	 *  searchFiles that return the list of fileNames that have that tag that 
	 *  wanted
	 *  
	 * @param strategyStr - the name of the strategy class
	 * @param tag - a tag that wanted from this strategy
	 * @return fileNames - list of strings - every one is a name of a file 
	 */
	public ArrayList<String> searchFiles(String strategyStr, String tag){
		//get the strategy Instance
		Strategy strategyInstance;
		ArrayList<String> fileNames = null;
		try {		
			
			//***********************************************************
			strategyInstance = new PluginChooser(strategyStr).getStrategyInstanse();
			//strategyInstance = getStrategy(strategyStr);
			
			//the function return the files with this tag
			fileNames  = strategyInstance.searchFiles(tag);
			return fileNames;
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return fileNames;		
	}
	//end fileNames
	
	
	/*
	//for debuging
	public Strategy getStrategy(String strategyName){
		if(strategyName.equals("TagBySize")){
			return strategiesList.get(0);
		}
		else if(strategyName.equals("TagByType")){
			return strategiesList.get(1);
		}
		else{
			return strategiesList.get(2);
		}
	}
	*/
}
